import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:myturngame/Theme.dart';
import 'package:myturngame/widget/text_decoration.dart';
import 'package:oktoast/oktoast.dart';

import '../bean/role.dart';

///角色信息布局
class RoleInfoWidget extends StatefulWidget {
  Role role;

  RoleInfoWidget(this.role, {Key? key}) : super(key: key);

  @override
  State<RoleInfoWidget> createState() => _RoleInfoWidgetState();
}

class _RoleInfoWidgetState extends State<RoleInfoWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(20),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          //经验值
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              const Text("等级", style: textStyle2),
              const VerticalDivider(width: 5),
              BorderContainer(
                Text("${widget.role.level}"),
                width: 50,
              ),
            ],
          ),
          const Divider(height: 10, color: Colors.transparent),
          //气血
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              const Text("气血", style: textStyle2),
              const VerticalDivider(width: 5),
              Expanded(
                flex: 5,
                child: BorderContainer(
                  Text("${widget.role.finalMaxHp()}/${widget.role.hp}"),
                ),
              ),
            ],
          ),
          const Divider(height: 10, color: Colors.transparent),
          //法力
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              const Text("法力", style: textStyle2),
              const VerticalDivider(width: 5),
              Expanded(
                  flex: 5,
                  child: BorderContainer(
                    Text(
                        "${widget.role.finalMaxMp()}/${widget.role.mp}"),
                  ))
            ],
          ),
          const Divider(height: 10, color: Colors.transparent),
          //攻击
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              const Text("攻击", style: textStyle2),
              const VerticalDivider(width: 5),
              Expanded(
                  flex: 3,
                  child: BorderContainer(
                    Text(widget.role.finalAttack().toString()),
                  )),
              const Spacer(),
              AttributeLayout(
                  "力量",
                  widget.role.finalAttrList[AttributeSet.pointIndexPower], () {
                addAttrPoint(widget.role, AttributeSet.pointIndexPower);
              }, () {
                minusAttrPoint(widget.role, AttributeSet.pointIndexPower);
              })
            ],
          ),
          //法力
          const Divider(height: 10, color: Colors.transparent),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              const Text("法伤", style: textStyle2),
              const VerticalDivider(width: 5),
              Expanded(
                  flex: 3,
                  child: BorderContainer(
                    Text(widget.role.finalMagicAttack().toString()),
                  )),
              const Spacer(),
              AttributeLayout(
                  "魔力",
                  widget.role.finalAttrList[AttributeSet.pointIndexMagic], () {
                addAttrPoint(widget.role, AttributeSet.pointIndexMagic);
              }, () {
                minusAttrPoint(widget.role, AttributeSet.pointIndexMagic);
              })
            ],
          ),
          //防御
          const Divider(height: 10, color: Colors.transparent),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              const Text("防御", style: textStyle2),
              const VerticalDivider(width: 5),
              Expanded(
                  flex: 3,
                  child: BorderContainer(
                    Text(widget.role.finalDefend().toString()),
                  )),
              const Spacer(),
              AttributeLayout(
                  "耐力",
                  widget.role.finalAttrList[AttributeSet.pointIndexPatience], () {
                addAttrPoint(widget.role, AttributeSet.pointIndexPatience);
              }, () {
                minusAttrPoint(widget.role, AttributeSet.pointIndexPatience);
              })
            ],
          ),
          //速度
          const Divider(height: 10, color: Colors.transparent),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              const Text("速度", style: textStyle2),
              const VerticalDivider(width: 5),
              Expanded(
                  flex: 3,
                  child: BorderContainer(
                    Text(widget.role.finalSpeed().toString()),
                  )),
              const Spacer(),
              AttributeLayout(
                  "敏捷",
                  widget.role.finalAttrList[AttributeSet.pointIndexAgility], () {
                addAttrPoint(widget.role, AttributeSet.pointIndexAgility);
              }, () {
                minusAttrPoint(widget.role, AttributeSet.pointIndexAgility);
              })
            ],
          ),
          //体力
          const Divider(height: 10, color: Colors.transparent),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              const Text("暴击", style: textStyle2),
              const VerticalDivider(width: 5),
              Expanded(
                  flex: 3,
                  child: BorderContainer(
                    Text("${widget.role.finalCriticalRate()}%"),
                  )),
              const Spacer(),
              AttributeLayout(
                  "体力",
                  widget.role.finalAttrList[AttributeSet.pointIndexHealth], () {
                addAttrPoint(widget.role, AttributeSet.pointIndexHealth);
              }, () {
                minusAttrPoint(widget.role, AttributeSet.pointIndexHealth);
              })
            ],
          ),
          //可分配属性点
          const Divider(height: 10, color: Colors.transparent),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              const Text("暴伤", style: textStyle2),
              const VerticalDivider(width: 5),
              Expanded(
                  flex: 3,
                  child: BorderContainer(
                    Text("${widget.role.finalCriticalDamage()}%"),
                  )),
              const Spacer(),
              const Text("可分配属性点", style: textStyle2),
              const VerticalDivider(width: 5),
              BorderContainer(
                Text(widget.role.allotAttrPoint.toString()),
                width: 55,
              ),
            ],
          ),
          //经验
          const Divider(height: 10, color: Colors.transparent),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              const Text("经验", style: textStyle2),
              const VerticalDivider(width: 5),
              Expanded(
                  flex: 10,
                  child: BorderContainer(
                    Text("${widget.role.exp}/${widget.role.maxExp}"),
                  )),
              const Spacer(),
              InkWell(
                onTap: () {
                  attemptLevel(widget.role);
                },
                child: BorderContainer(
                  const Text("升级", style: textStyle3),
                  alignment: Alignment.center,
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  void addAttrPoint(Role player, int pointAttrIndex) {
    setState(() {
      player.addAttributePoint(pointAttrIndex);
    });
  }

  void minusAttrPoint(Role player, int pointAttrIndex) {
    setState(() {
      player.minusAttributePoint(pointAttrIndex);
    });
  }

  ///尝试升级
  void attemptLevel(Role role) {
    if (role.exp >= role.maxExp) {
      debugPrint("等级:${role.level} 经验：${role.exp} 最大:${role.maxExp}");
      setState(() {
        role.levelUp();
      });
    } else {
      Fluttertoast.showToast(msg: "经验值不足", gravity: ToastGravity.CENTER);
    }
  }
}

///属性布局
class AttributeLayout extends StatelessWidget {
  String name;
  int attribute;
  Function() clickAdd;
  Function() clickMinus;

  //朝着某个方向移动的位移累计值，达到上限就出发增加或减少，并重置
  double moveValue = 0.0;

  AttributeLayout(this.name, this.attribute, this.clickAdd, this.clickMinus,
      {Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        children: [
          Text(name, style: textStyle2),
          const VerticalDivider(width: 5),
          GestureDetector(
            onPanUpdate: (DragUpdateDetails e) {
              moveValue += e.delta.dx;

              int threshold = 5;
              if (moveValue > threshold) {
                clickAdd();
                moveValue=0;
              } else if(moveValue < -threshold){
                clickMinus();
                moveValue=0;
              }
            },
            child: BorderContainer(
              Text(attribute.toString()),
              width: 50,
            ),
          ),
          const VerticalDivider(width: 5),
          InkWell(
            onTap: clickAdd,
            child: BorderContainer(
              const Text("+"),
              width: 30,
              alignment: Alignment.center,
            ),
          ),
          const VerticalDivider(width: 5),
          InkWell(
            onTap: clickMinus,
            child: BorderContainer(
              const Text("-"),
              width: 30,
              alignment: Alignment.center,
            ),
          )
        ],
      ),
    );
  }
}
